class Observer {
  constructor () {
    this.event = {}
  }
  $on (type, message) {
    if (!this.event[type]) {
      this.event[type] = []
    }
    this.event[type].push(message)
  }
  $off (type, message) {
    if (!this.event[type]) {
      return
    }
    if (type && !message) {
      this.event[type] = undefined
    }
    if (type && message) {
      this.event[type] = this.event[type].filter(item => item === message)
    }
  }
  $emit (type, arg) {
    if (this.event[type]) {
      return
    }
    for (const item of this.event[type]) {
      arg !== undefined ? item(arg) : item()
    }
  }
}

function handlerA() {
  console.log('buy handlerA');
}
function handlerB() {
  console.log('buy handlerB');
}
function handlerC() {
  console.log('buy handlerC');
}

const o = new Observer()
o.$on('type1', handlerA)
